﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class VendorInvoiceController : ControllerBase
    {

        public void addVendorInvoice(string stockInNo, int stockInId, int paid)
        {
            VendInvoice vendInvoice=new VendInvoice();
            vendInvoice.No = stockInNo;
            vendInvoice.StockInId= stockInId;
            vendInvoice.Paid =(byte)paid;
            vendInvoice.Remark = "";
            Sdc.VendInvoices.InsertOnSubmit(vendInvoice);
            Sdc.SubmitChanges();

        }



        public IList<VendorInvoiceEntity> FindAllVendorInvoices()
        {
            IList<VendInvoice> vendInvoices = (from v in Sdc.VendInvoices
                                               orderby v.StockIn.Date descending,v.StockInId descending 
                                               select v).ToList();
            IList<VendorInvoiceEntity> vendorInvoiceEntities=new List<VendorInvoiceEntity>();
            foreach (VendInvoice vendInvoice in vendInvoices)
            {
                VendorInvoiceEntity vendorInvoiceEntity = TransformVendorInvoiceToVendorInvoiceEntity(vendInvoice);
                vendorInvoiceEntities.Add(vendorInvoiceEntity);
            }
            return vendorInvoiceEntities;
        }

        public IList<VendorInvoiceEntity> FindVendorInvoiceByDate(DateTime fromDate, DateTime toDate)
        {
            IList<VendInvoice> vendInvoices = (from v in Sdc.VendInvoices
                                               where v.StockIn.Date >= fromDate && v.StockIn.Date <= toDate
                                               orderby v.StockIn.Date descending, v.StockInId descending
                                               select v).ToList();
            IList<VendorInvoiceEntity> vendorInvoiceEntities = new List<VendorInvoiceEntity>();
            foreach (VendInvoice vendInvoice in vendInvoices)
            {
                VendorInvoiceEntity vendorInvoiceEntity = TransformVendorInvoiceToVendorInvoiceEntity(vendInvoice);
                vendorInvoiceEntities.Add(vendorInvoiceEntity);
            }
            return vendorInvoiceEntities;

        }

        public IList<VendorInvoiceEntity> FindVendorInvoiceByInvoiceNo(string invoiceNo)
        {
            IList<VendInvoice> vendInvoices = (from v in Sdc.VendInvoices
                                               where v.No.ToUpper().Contains(invoiceNo)
                                               orderby v.StockIn.Date descending, v.StockInId descending
                                               select v).ToList();
            IList<VendorInvoiceEntity> vendorInvoiceEntities = new List<VendorInvoiceEntity>();
            foreach (VendInvoice vendInvoice in vendInvoices)
            {
                VendorInvoiceEntity vendorInvoiceEntity = TransformVendorInvoiceToVendorInvoiceEntity(vendInvoice);
                vendorInvoiceEntities.Add(vendorInvoiceEntity);
            }
            return vendorInvoiceEntities;
        }

        public IList<VendorInvoiceEntity> FindVendorInvoiceByVendor(int vendorId)
        {
            IList<VendInvoice> vendInvoices = (from v in Sdc.VendInvoices
                                               where v.StockIn.VendId == vendorId
                                               orderby v.StockIn.Date descending, v.StockInId descending
                                               select v).ToList();
            IList<VendorInvoiceEntity> vendorInvoiceEntities = new List<VendorInvoiceEntity>();
            foreach (VendInvoice vendInvoice in vendInvoices)
            {
                VendorInvoiceEntity vendorInvoiceEntity = TransformVendorInvoiceToVendorInvoiceEntity(vendInvoice);
                vendorInvoiceEntities.Add(vendorInvoiceEntity);
            }
            return vendorInvoiceEntities;
        }

        public static VendorInvoiceEntity TransformVendorInvoiceToVendorInvoiceEntity(VendInvoice vendInvoice)
        {
            VendorController vendorController=new VendorController();
            VendorEntity vendorEntity = vendorController.findVendorByVendorId(vendInvoice.StockIn.VendId);
            VendorInvoiceEntity vendorInvoiceEntity=new VendorInvoiceEntity();
            vendorInvoiceEntity.VendorInvoiceDate = vendInvoice.StockIn.Date;
            vendorInvoiceEntity.VendorInvoiceNo = vendInvoice.No;
            vendorInvoiceEntity.VendorShortName = vendorEntity.ShortName;
            vendorInvoiceEntity.VendorInvoiceId = vendInvoice.Id;
            
            if(vendInvoice.Paid==1)
            {
                vendorInvoiceEntity.Paid = "Yes";
            }
            if(vendInvoice.Paid==0)
            {
                vendorInvoiceEntity.Paid = "No";
            }
            return vendorInvoiceEntity;

        }

        public VendorInvoiceEntity FindVendorInvoiceById(int vendorInvoiceId)
        {
            VendInvoice vendInvoice = (from v in Sdc.VendInvoices
                                       where v.Id == vendorInvoiceId
                                       select v).SingleOrDefault();
            VendorInvoiceEntity vendorInvoiceEntity = TransformVendorInvoiceToVendorInvoiceEntity(vendInvoice);
            return vendorInvoiceEntity;
        }

        public int DeleteVendorInvoiceById(int vendorInvoiceId)
        {
            try
            {
                //Delete vendor invoice
                VendInvoice vendInvoice = (from v in Sdc.VendInvoices
                                           where v.Id == vendorInvoiceId
                                           select v).SingleOrDefault();
                Sdc.VendInvoices.DeleteOnSubmit(vendInvoice);
                //Sdc.SubmitChanges();

                //Delete stock in
                StockIn stockIn = (from s in Sdc.StockIns
                                   where s.Id == vendInvoice.StockInId
                                   select s).SingleOrDefault();
                Sdc.StockIns.DeleteOnSubmit(stockIn);
                //Sdc.SubmitChanges();

                //Update storage
                IList<Storage> storages = (from s in Sdc.Storages
                                   where s.StockInId == stockIn.Id
                                   select s).ToList();
                foreach (Storage storage in storages)
                {
                    Sdc.Storages.DeleteOnSubmit(storage);
                    //Delete stock item
                    int qty = storage.Qty;
                    StockItem stockItem = (from si in Sdc.StockItems
                                           where si.Id == storage.StockItemId
                                           select si).SingleOrDefault();
                    stockItem.Stock = stockItem.Stock - qty;

                    //Update vendor po item and vendor po
                    VendPOItem vendPoItem = (from vi in Sdc.VendPOItems
                                             where vi.Id == storage.VendPOItemId
                                             select vi).SingleOrDefault();
                    vendPoItem.CompleteQty = vendPoItem.CompleteQty - qty;
                    vendPoItem.Status = "vendor PO";

                    VendPO vendPo = (from vp in Sdc.VendPOs
                                     where vp.Id == vendPoItem.VendPOId
                                     select vp).SingleOrDefault();
                    vendPo.Status = "new";

                    //Update product stock
                    Product product = (from p in Sdc.Products
                                       where p.Id == stockItem.ProductId
                                       select p).SingleOrDefault();
                    product.Stock = product.Stock - qty;
                    //Sdc.SubmitChanges();
                }

                Sdc.SubmitChanges();
                return 1;
            }
            catch(Exception ex)
            {
                return 0;
            }
        }

        public IList<StorageEntity> FindVendorInvoiceStorages(int vendorInvoiceId)
        {
            StockIn stockIn = (from v in Sdc.VendInvoices
                               where v.Id == vendorInvoiceId
                               select v.StockIn).SingleOrDefault();
            IList<Storage> storages = (from s in Sdc.Storages
                                       where s.StockInId == stockIn.Id
                                       select s).ToList();
            IList<StorageEntity> storageEntities=new List<StorageEntity>();
            foreach (Storage storage in storages)
            {
                StorageEntity storageEntity = TransformStorageToStorageEntity(storage);
                storageEntities.Add(storageEntity);

            }
            return storageEntities;
        }

        public static StorageEntity TransformStorageToStorageEntity(Storage storage)
        {
            StorageEntity storageEntity=new StorageEntity();
            storageEntity.ProductNo = storage.StockItem.Product.No;
            storageEntity.ProductName = storage.StockItem.Product.Name;
            storageEntity.Revision = storage.StockItem.Product.Revision;
            storageEntity.Quantity = storage.Qty;
            return storageEntity;
        }
    }
}
